<template>
  <div class="found">
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="(item, index) in bannerData" :key="index" :style="{ background: item.bg }">
        <img :src="item.img" />
      </van-swipe-item>
    </van-swipe>
    <div class="typeList">
      <div class="item" v-for="(item, index) in classListData" :key="index" @click="clickGo(index, item.path)">
        <img :src="item.img" alt />
        <p>{{ item.name }}</p>
      </div>
    </div>
    <div class="potent-box">
      <div class="potent-top">
        <h3>
          潜力新作
          <img src="../assets/qianli.png" alt />
        </h3>
        <span>
          <van-icon name="replay" @click="changeTheNovel()" />换一换
        </span>
      </div>
      <div class="potent-bottom">
        <div class="potent-item" v-for="(item, index) in pontentData" :key="index">
          <div class="item-img" @click=" linkToDetail(item.comic_id)">
            <img v-lazy="item.cover" alt />
          </div>
          <div class="item-text">
            <h3>{{ item.comic_title }}</h3>
            <span>更新至{{ item.last_short_title }}级</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getBanner, getPotent, getcomicDetail } from "@/api/api";
import { classListData } from "../js/data";
export default {
  name: "Found",
  data() {
    return {
      bannerData: [],
      pontentData: [],
      classListData,
      itemIndex: -1
    };
  },
  created() {
    //获取轮播banner图
    getBanner()
      .then(res => {
        if (res.data.code === 0) {
          this.bannerData = res.data.data;
        }
      })
      .catch(err => {
        console.log(err);
      });
    //获取潜力新作的对应小说数据
    this.changeTheNovel()
  },
  methods: {
    //点击跳转不同主题页面
    clickGo(index, path) {
      if (this.itemIndex === index) {
        return;
      }
      this.itemIndex = index
      this.$router.push({ path, query: { path: this.$route.fullPath } });
    },
    //获取漫画小说的id跳转到详情页
    linkToDetail(id) {
      let path = "/detail";
      this.$router.push({
        path,
        query: { comicId: id, path: this.$route.fullPath }
      });
    },
    //换一换
    changeTheNovel() {
      // let num = [1018,1019]
      // num.sort(()=>{
      //     return Math.random()-0.5
      // })
      // num=num.slice(0,1)
      let num = Math.floor(Math.random() * (1035 - 1018 + 1) + 1018);
      getPotent(num)
        .then(res => {
          if (res.data.data.rank.length > 0) {
            this.pontentData = res.data.data.rank.slice(0, 9);
            console.log(res.data.data.rank)
          }
        })
        .catch(err => {
          console.log(err);
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.found {
  margin-top: 10px;
  padding-bottom: 50px;

  .my-swipe {
    width: 90%;
    margin: 0 auto;
    border-radius: 20px;

    .van-swipe-item {
      color: #fff;
      font-size: 20px;
      line-height: 150px;
      text-align: center;
      background-color: #39a9ed;
      height: 169px;
    }

    img {
      width: 100%;
    }
  }

  .typeList {
    display: flex;
    justify-content: space-around;
    margin-top: 10px;

    .item {
      width: 20%;
      text-align: center;

      p {
        margin-top: 5px;
      }
    }
  }

  .potent-box {
    .potent-top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 10px;

      h3 {
        font-size: 18px;

        img {
          width: 18px;
          vertical-align: -2px;
          margin-left: 5px;
        }
      }

      span {
        font-size: 14px;
        cursor: pointer;
        border: 1px solid #1989fa;
        border-radius: 10px;
        padding: 2px;
        line-height: 16px;
      }
    }

    .potent-bottom {
      display: flex;
      flex-wrap: wrap;

      .potent-item {
        width: 50%;
        box-sizing: border-box;
        padding: 0 10px;
        margin: 10px 0;

        &:first-child {
          margin-top: 0;
          width: 100%;
        }

        .item-img {
          width: 100%;

          img {
            width: 100%;
            border-radius: 10px;
          }
        }

        .item-text {
          display: flex;
          align-items: center;
          padding: 0 5px;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;

          h3 {
            white-space: nowrap;
            text-overflow: ellipsis;
            overflow: hidden;
            margin: 0;
            font-size: 14px;
            color: #333;
          }

          span {
            text-align: right;
            margin-left: auto;
            color: #777;
          }
        }
      }
    }
  }
}
</style>